﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using Cherish.Core.Externs;

namespace Cherish.LScheduler.Data.Sqlite
{
    public class ActivityAccessor : IActivityAccessor
    {
        public int Create(IActivity activity)
        {
            var parameters = new List<SQLiteParameter>();
            parameters.Add(new SQLiteParameter("@subject", activity.Subject));
            parameters.Add(new SQLiteParameter("@content", activity.Content));
            parameters.Add(new SQLiteParameter("@starttime", activity.StartTime));
            parameters.Add(new SQLiteParameter("@endtime", activity.EndTime));
            parameters.Add(new SQLiteParameter("@color", activity.Color));
            return SqliteManager.ExecuteInsert("insert into activity(subject,content,starttime,endtime,color) values(@subject,@content,@starttime,@endtime,@color)", parameters.ToArray());
        }

        public void Update(IActivity activity)
        {
            throw new NotImplementedException();
        }

        public void Delete(int activityID)
        {
            throw new NotImplementedException();
        }

        public List<IActivity> GetList(ActivityCriterion criteria)
        {
            var parameters = new List<SQLiteParameter>();
            var sbSQL = new StringBuilder("select * from activity where 1=1");
            if (criteria.StartTime != null)
            {
                parameters.Add(new SQLiteParameter("@starttime", criteria.StartTime));
                sbSQL.Append(" and starttime>=@starttime");
            }
            if (criteria.EndTime != null)
            {
                parameters.Add(new SQLiteParameter("@endtime", criteria.EndTime));
                sbSQL.Append(" and endtime<=@endtime");
            }
            return SqliteManager.ExecuteDataTable(sbSQL.ToString(), parameters.ToArray()).ToList<IActivity>();
        }
    }
}
